package com.lance.shiro.web;

import com.lance.shiro.entity.EmailGroup;
import com.lance.shiro.service.UMailService;
import com.lance.shiro.service.UserService;
import com.lance.shiro.utils.RequestUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


/**
 * Created by bingyun on 2018-06-10.
 */
@RestController
@RequestMapping("/rest/mail")
public class MailController {

    public Logger log = LogManager.getLogger(getClass());

    @Autowired
    private UMailService uMailService;

    @RequestMapping(value = "", method = RequestMethod.GET)
    public ResponseEntity mail() throws Exception {
//        uMailService.addMailBox("test123","test123","iPanPassw0rd");
//        uMailService.delMailbox("test123");
        uMailService.sendManagerMail("eric5310@163.com", "test1", "test123");
        return RequestUtils.success("ok");
    }


    @RequestMapping(value = "broadcast", method = RequestMethod.POST)
    public ResponseEntity mailBroadCasting(@RequestBody EmailGroup json) throws Exception {

        log.debug("mailBroadCasting with body {} of type {}", json, json.getClass());

        Map ret = uMailService.sendManagerMailByGroup(json);

        if (ret != null && ret.containsKey("status") && ret.get("status").equals("0")) {
            return RequestUtils.success("ok", ret);
        } else {
            return RequestUtils.error("err", ret);
        }
    }

}
